package io.geekidea.boot.notice.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.geekidea.boot.common.GroupValidator;
import io.geekidea.boot.common.PageDto;
import io.geekidea.boot.framework.response.ApiResult;
import io.geekidea.boot.notice.dto.NoticeDto;
import io.geekidea.boot.notice.entity.Notice;
import io.geekidea.boot.notice.query.NoticeQuery;
import io.geekidea.boot.notice.service.NoticeService;
import io.geekidea.boot.notice.vo.NoticeVo;
import io.geekidea.boot.salesslip.entity.Salesslip;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@RestController
//@Controller
//@ResponseBody
//@RequestMapping(method = RequestMethod.GET) <=>  @GetMapping
//@RequestMapping(method = RequestMethod.POST) <=> @PostMapping
//@RequestMapping(method = RequestMethod.PUT) <=> @PutMapping
//@RequestMapping(method = RequestMethod.DELETE) <=>  @DeleteMapping
@RequestMapping("/notice")
@Tag(name = "公告管理")
public class NoticeController {

    @Resource
    private NoticeService noticeService;

    @Operation(summary = "查询公告")
    @PostMapping("/search")  // JSON对象  @RequestBody
    public ApiResult<PageDto<Notice>> searchNotice(@RequestBody NoticeQuery noticeQuery) {
//        Page<Notice> page = new Page<>(noticeQuery.getPageIndex(), noticeQuery.getPageSize());
//        // 筛选条件
//        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
//        //公告类型(总部公告or战区公告)  动态条件查询
//        queryWrapper.eq(
//                Objects.nonNull(noticeQuery)
//                        && StringUtils.isNotBlank(noticeQuery.getType()),
//                "type",
//                noticeQuery.getType()
//        );
//        queryWrapper.ge(Objects.nonNull(noticeQuery) && StringUtils.isNotBlank(noticeQuery.getStartTime()), "createdTime", noticeQuery.getStartTime());
//        queryWrapper.le(Objects.nonNull(noticeQuery) && StringUtils.isNotBlank(noticeQuery.getEndTime()), "createdTime", noticeQuery.getEndTime());
//        queryWrapper.orderByDesc("createdTime");
//        final IPage<Notice> pageVo = noticeService.page(page, queryWrapper);
//
//        PageDto pageDto = new PageDto();
//        pageDto.setCurrent(pageVo.getCurrent());
//        pageDto.setList(pageVo.getRecords());
//        pageDto.setSize(pageVo.getSize());
//        pageDto.setTotal(pageVo.getTotal());
//        return ApiResult.success(pageDto);
        return null;
    }

    @Operation(summary = "按条件分页查询公告")
    @PostMapping("/selectPage")  // JSON对象  @RequestBody
    public ApiResult  listPage(@RequestBody NoticeQuery noticeQuery) {
        Page<NoticeVo> page=new Page<>(noticeQuery.getPage(),noticeQuery.getSize());
        Page<NoticeVo> pageDto=noticeService.findByPage(page,noticeQuery);
        PageDto<NoticeVo> pageInfo=new PageDto<>(pageDto.getRecords(),pageDto.getTotal(),pageDto.getSize(),pageDto.getCurrent());
        return ApiResult.success(pageInfo);

    }

    @Operation(summary = "新增公告")
    @PostMapping("/add")
    public ApiResult addNotice(@RequestBody @Validated(value = GroupValidator.Insert.class) NoticeDto noticeDto) {
        Notice noticeEntity = new Notice();
        BeanUtils.copyProperties(noticeDto,noticeEntity);
        noticeService.save(noticeEntity);
        return ApiResult.success("新增公告成功");
    }

    @Operation(summary = "修改公告")
    @PostMapping("/update")
    public ApiResult updateNotice(@RequestBody @Validated(value = GroupValidator.Update.class) NoticeDto noticeDto) {
        Notice noticeEntity = new Notice();
        BeanUtils.copyProperties(noticeDto,noticeEntity);
        noticeService.updateById(noticeEntity);
        return ApiResult.success("修改公告成功");
    }

    @Operation(summary = "获取公告")
    @PostMapping("/get/{id}")
    public ApiResult getNotice(@PathVariable(value = "id") Integer id) {
        Notice notice = noticeService.getById(id);
        NoticeVo noticeVo = new NoticeVo();
        BeanUtils.copyProperties(notice, noticeVo);
        return ApiResult.success(noticeVo);
    }


    @Operation(summary = "删除公告")
    @PostMapping("/delete/{id}")
    public ApiResult deleteNotice(@PathVariable(value = "id") Integer id) {
        noticeService.removeById(id);
        return ApiResult.success("删除公告成功");
    }

}
